package cn.edu.neusoft.yuejian4.yuejian_4.fragment;



import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MyCollectionTrainAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Train;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.List_TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.TrainModel;

public class TrainCollectionFragment extends Fragment implements View.OnClickListener {

    private View view;
    private RecyclerView recyclerView;
    private SharedPreferences sp;
    private int uid;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_collection_train,container,false);
        sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        uid = sp.getInt("uid",4);
        initView();
        initEvent();
        MyModel myModel = new MyModel();
        myModel.mycollTrain(uid,myCollTrainLis);
        return view;
    }

    List_TListener<Train> myCollTrainLis = new List_TListener<Train>() {
        @Override
        public void onResponse(final List<Train> list) {
            MyCollectionTrainAdapter myCollectionTrainAdapter = new MyCollectionTrainAdapter(getContext(),list);
            recyclerView.setAdapter(myCollectionTrainAdapter);
            myCollectionTrainAdapter.setmOnItemClickListener(new MyCollectionTrainAdapter.mOnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    switch (view.getId()) {
                        case R.id.uncollection_trian_btn:
                            TrainModel trainModel = new TrainModel();
                            trainModel.uncollection(list.get(position).getId(), uid, new TListener<Success>() {
                                @Override
                                public void onResponse(Success success) {
                                    if(success.getError().equals("0")){
                                        Toast.makeText(getActivity(),"取消收藏成功！", Toast.LENGTH_SHORT).show();
                                        refresh();
                                    }

                                }

                                @Override
                                public void onFail(String msg) {

                                }
                            });
                            break;
                    }
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };
    public void refresh() {
//        Intent intent = new Intent(MyCollectionActivity.this, MyCollectionActivity.class);
//        startActivity(intent);
//        finish();
    }
    private void initView(){
        recyclerView = (RecyclerView)view.findViewById(R.id.rv_my_coll_train);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swiper_my_coll_train);
        swipeRefreshLayout.setColorSchemeColors(Color.GREEN);

    }

    private void initEvent() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                MyModel myModel = new MyModel();
                myModel.mycollTrain(uid,myCollTrainLis);
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }

    @Override
    public void onClick(View v) {

    }
}
